1
|
|
|
var utils = require('../utils'), |
2
|
|
|
GedcomX = require('../'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A GEDCOM X document. |
6
|
|
|
* |
7
|
|
|
* @constructor |
8
|
|
|
* @param {Object} [json] |
|
|
|
|
9
|
|
|
*/ |
10
|
|
|
var Root = function(json){ |
11
|
|
|
|
12
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
13
|
|
|
if(!(this instanceof Root)){ |
14
|
|
|
return new Root(json); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
18
|
|
|
if(Root.isInstance(json)){ |
19
|
|
|
return json; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
this.init(json); |
|
|
|
|
23
|
|
|
}; |
24
|
|
|
|
25
|
|
|
Root.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
26
|
|
|
|
27
|
|
|
Root._gedxClass = Root.prototype._gedxClass = 'Root'; |
28
|
|
|
|
29
|
|
|
Root.jsonProps = [ |
30
|
|
|
'lang', |
31
|
|
|
'description', |
32
|
|
|
'persons', |
33
|
|
|
'relationships', |
34
|
|
|
'sourceDescriptions', |
35
|
|
|
'agents', |
36
|
|
|
'events', |
37
|
|
|
'documents', |
38
|
|
|
'places', |
39
|
|
|
'attribution' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check whether the given object is an instance of this class. |
44
|
|
|
* |
45
|
|
|
* @param {Object} obj |
46
|
|
|
* @returns {Boolean} |
47
|
|
|
*/ |
48
|
|
|
Root.isInstance = function(obj){ |
49
|
|
|
return utils.isInstance(obj, this._gedxClass); |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize from JSON |
54
|
|
|
* |
55
|
|
|
* @param {Object} |
|
|
|
|
56
|
|
|
* @return {Root} this |
57
|
|
|
*/ |
58
|
|
|
Root.prototype.init = function(json){ |
59
|
|
|
|
60
|
|
|
GedcomX.ExtensibleData.prototype.init.call(this, json); |
61
|
|
|
|
62
|
|
|
if(json){ |
63
|
|
|
this.setLang(json.lang); |
64
|
|
|
this.setPersons(json.persons); |
65
|
|
|
this.setRelationships(json.relationships); |
66
|
|
|
this.setSourceDescriptions(json.sourceDescriptions); |
67
|
|
|
this.setAgents(json.agents); |
68
|
|
|
this.setEvents(json.events); |
69
|
|
|
this.setDocuments(json.documents); |
70
|
|
|
this.setPlaces(json.places); |
71
|
|
|
this.setAttribution(json.attribution); |
72
|
|
|
this.setDescription(json.description); |
73
|
|
|
} |
74
|
|
|
return this; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the lang |
79
|
|
|
* |
80
|
|
|
* @return {String} |
81
|
|
|
*/ |
82
|
|
|
Root.prototype.getLang = function(){ |
83
|
|
|
return this.lang; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the lang |
88
|
|
|
* |
89
|
|
|
* @param {String} lang |
90
|
|
|
* @return {Root} This instance |
91
|
|
|
*/ |
92
|
|
|
Root.prototype.setLang = function(lang){ |
93
|
|
|
if(lang){ |
94
|
|
|
this.lang = lang; |
95
|
|
|
} |
96
|
|
|
return this; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the description |
101
|
|
|
* |
102
|
|
|
* @return {String} |
103
|
|
|
*/ |
104
|
|
|
Root.prototype.getDescription = function(){ |
105
|
|
|
return this.description; |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the description |
110
|
|
|
* |
111
|
|
|
* @param {String} description URI that must resolve to a SourceDescription |
112
|
|
|
* @return {Root} This instance |
113
|
|
|
*/ |
114
|
|
|
Root.prototype.setDescription = function(description){ |
115
|
|
|
if(description){ |
116
|
|
|
this.description = description; |
117
|
|
|
} |
118
|
|
|
return this; |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the persons |
123
|
|
|
* |
124
|
|
|
* @returns {Person[]} |
125
|
|
|
*/ |
126
|
|
|
Root.prototype.getPersons = function(){ |
127
|
|
|
return this.persons || []; |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the persons |
132
|
|
|
* |
133
|
|
|
* @param {Person[]|Object[]} persons |
134
|
|
|
* @returns {Root} This instance |
135
|
|
|
*/ |
136
|
|
|
Root.prototype.setPersons = function(persons){ |
137
|
|
|
return this._setArray(persons, 'persons', 'addPerson'); |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add a person |
142
|
|
|
* |
143
|
|
|
* @param {Person|Object} |
|
|
|
|
144
|
|
|
* @returns {Root} This instance |
145
|
|
|
*/ |
146
|
|
|
Root.prototype.addPerson = function(person){ |
147
|
|
|
return this._arrayPush(person, 'persons', GedcomX.Person); |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the relationships |
152
|
|
|
* |
153
|
|
|
* @returns {Relationship[]} |
154
|
|
|
*/ |
155
|
|
|
Root.prototype.getRelationships = function(){ |
156
|
|
|
return this.relationships || []; |
157
|
|
|
}; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set the relationships |
161
|
|
|
* |
162
|
|
|
* @param {Relationship[]|Object[]} relationships |
163
|
|
|
* @returns {Root} |
164
|
|
|
*/ |
165
|
|
|
Root.prototype.setRelationships = function(relationships){ |
166
|
|
|
return this._setArray(relationships, 'relationships', 'addRelationship'); |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Add a relationship |
171
|
|
|
* |
172
|
|
|
* @param {Relationship|Object} relationship |
173
|
|
|
* @returns {Root} |
174
|
|
|
*/ |
175
|
|
|
Root.prototype.addRelationship = function(relationship){ |
176
|
|
|
return this._arrayPush(relationship, 'relationships', GedcomX.Relationship); |
177
|
|
|
}; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the source descriptions |
181
|
|
|
* |
182
|
|
|
* @returns {SourceDescription[]} |
183
|
|
|
*/ |
184
|
|
|
Root.prototype.getSourceDescriptions = function(){ |
185
|
|
|
return this.sourceDescriptions || []; |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the source descriptions |
190
|
|
|
* |
191
|
|
|
* @param {SourceDescription[]|Object[]} sourceDescriptions |
192
|
|
|
* @returns {Root} |
193
|
|
|
*/ |
194
|
|
|
Root.prototype.setSourceDescriptions = function(sourceDescriptions){ |
195
|
|
|
return this._setArray(sourceDescriptions, 'sourceDescriptions', 'addSourceDescription'); |
196
|
|
|
}; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add a ource description |
200
|
|
|
* |
201
|
|
|
* @param {SourceDescription|Object} sourceDescription |
202
|
|
|
* @returns {Root} |
203
|
|
|
*/ |
204
|
|
|
Root.prototype.addSourceDescription = function(sourceDescription){ |
205
|
|
|
return this._arrayPush(sourceDescription, 'sourceDescriptions', GedcomX.SourceDescription); |
206
|
|
|
}; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the agents |
210
|
|
|
* |
211
|
|
|
* @returns {Agent[]} |
212
|
|
|
*/ |
213
|
|
|
Root.prototype.getAgents = function(){ |
214
|
|
|
return this.agents || []; |
215
|
|
|
}; |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set the agents |
219
|
|
|
* |
220
|
|
|
* @param {Agent[]|Object[]} agents |
221
|
|
|
* @returns {Root} |
222
|
|
|
*/ |
223
|
|
|
Root.prototype.setAgents = function(agents){ |
224
|
|
|
return this._setArray(agents, 'agents', 'addAgent'); |
225
|
|
|
}; |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Add an agent |
229
|
|
|
* |
230
|
|
|
* @param {Agent|Object} |
|
|
|
|
231
|
|
|
* @returns {Root} |
232
|
|
|
*/ |
233
|
|
|
Root.prototype.addAgent = function(agent){ |
234
|
|
|
return this._arrayPush(agent, 'agents', GedcomX.Agent); |
235
|
|
|
}; |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get events |
239
|
|
|
* |
240
|
|
|
* @returns {Event[]} |
241
|
|
|
*/ |
242
|
|
|
Root.prototype.getEvents = function(){ |
243
|
|
|
return this.events || []; |
244
|
|
|
}; |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set events |
248
|
|
|
* |
249
|
|
|
* @param {Event[]|Object[]} events |
250
|
|
|
* @returns {Root} |
251
|
|
|
*/ |
252
|
|
|
Root.prototype.setEvents = function(events){ |
253
|
|
|
return this._setArray(events, 'events', 'addEvent'); |
254
|
|
|
}; |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Add an event |
258
|
|
|
* |
259
|
|
|
* @param {Event|Object} event |
260
|
|
|
* @returns {Root} |
261
|
|
|
*/ |
262
|
|
|
Root.prototype.addEvent = function(event){ |
263
|
|
|
return this._arrayPush(event, 'events', GedcomX.Event); |
264
|
|
|
}; |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get the documents |
268
|
|
|
* |
269
|
|
|
* @returns {Document[]} |
270
|
|
|
*/ |
271
|
|
|
Root.prototype.getDocuments = function(){ |
272
|
|
|
return this.documents || []; |
273
|
|
|
}; |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set the documents |
277
|
|
|
* |
278
|
|
|
* @param {Documents[]|Object[]} documents |
279
|
|
|
* @returns {Root} |
280
|
|
|
*/ |
281
|
|
|
Root.prototype.setDocuments = function(documents){ |
282
|
|
|
return this._setArray(documents, 'documents', 'addDocument'); |
283
|
|
|
}; |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Add a document |
287
|
|
|
* |
288
|
|
|
* @param {Document|Object} doc |
289
|
|
|
* @returns {Root} |
290
|
|
|
*/ |
291
|
|
|
Root.prototype.addDocument = function(doc){ |
292
|
|
|
return this._arrayPush(doc, 'documents', GedcomX.Document); |
293
|
|
|
}; |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get places |
297
|
|
|
* |
298
|
|
|
* @returns {PlaceDescription[]} |
299
|
|
|
*/ |
300
|
|
|
Root.prototype.getPlaces = function(){ |
301
|
|
|
return this.places || []; |
302
|
|
|
}; |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Set the places |
306
|
|
|
* |
307
|
|
|
* @param {PlaceDescription[]|Object} places |
308
|
|
|
* @returns {Root} |
309
|
|
|
*/ |
310
|
|
|
Root.prototype.setPlaces = function(places){ |
311
|
|
|
return this._setArray(places, 'places', 'addPlace'); |
312
|
|
|
}; |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Add a place |
316
|
|
|
* |
317
|
|
|
* @param {PlaceDescription} place |
318
|
|
|
* @returns {Root} |
319
|
|
|
*/ |
320
|
|
|
Root.prototype.addPlace = function(place){ |
321
|
|
|
return this._arrayPush(place, 'places', GedcomX.PlaceDescription); |
322
|
|
|
}; |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get attritbution |
326
|
|
|
* |
327
|
|
|
* @returns {Attribution} |
328
|
|
|
*/ |
329
|
|
|
Root.prototype.getAttribution = function(){ |
330
|
|
|
return this.attribution; |
331
|
|
|
}; |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Set attribution |
335
|
|
|
* |
336
|
|
|
* @param {Attribution} attribution |
337
|
|
|
* @returns {Root} |
338
|
|
|
*/ |
339
|
|
|
Root.prototype.setAttribution = function(attribution){ |
340
|
|
|
if(attribution){ |
341
|
|
|
this.attribution = GedcomX.Attribution(attribution); |
342
|
|
|
} |
343
|
|
|
return this; |
344
|
|
|
}; |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Export the object as JSON |
348
|
|
|
* |
349
|
|
|
* @return {Object} JSON object |
350
|
|
|
*/ |
351
|
|
|
Root.prototype.toJSON = function(){ |
352
|
|
|
return this._toJSON(GedcomX.ExtensibleData, Root.jsonProps); |
353
|
|
|
}; |
354
|
|
|
|
355
|
|
|
module.exports = Root; |